//
//  RegisterViewController.m
//  编程题1
//
//  Created by haoran on 16/3/30.
//  Copyright © 2016年 xuhaoran. All rights reserved.
//

#import "RegisterViewController.h"
#import "SVProgressHUD.h"
#import "AFNetworking.h"

@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;

@property (weak, nonatomic) IBOutlet UITextField *pwdTF;

@property (weak, nonatomic) IBOutlet UITextField *checkTF;

/** 用户名 **/
@property (nonatomic, strong) NSString *userName;
/** 密码 **/
@property (nonatomic, strong) NSString *pwd;

@end


@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)registerBtnClick:(id)sender {
   
    //1.判断用户名
    if ([_userNameTF.text isEqualToString:@""]) {
        //黑色遮罩
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showErrorWithStatus:@"请输入用用户名"];
        return;
    }
     _userName = _userNameTF.text;
    
    //2.判断密码
    if( [_pwdTF.text isEqualToString:@""]){
        //黑色遮罩
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showErrorWithStatus:@"请输入用密码"];
        return;
    }
    _pwd = _pwdTF.text;
    //3.判断验证密码
        //为空
    if ([_checkTF.text isEqualToString:@""]) {
        //黑色遮罩
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showErrorWithStatus:@"请重复输入用密码"];
        return;
    }
        //是否一致
    if ([_checkTF.text isEqualToString:_pwd]) {
        //黑色遮罩
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showWithStatus:@"正在注册"];
        //发送网络请求
        [self startRegist];
        
        
        
    }else{
        //黑色遮罩
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showErrorWithStatus:@"两次密码不一致,请重新输入"];
    }
    
    
}


- (IBAction)jumpBtnClick {
    
}

#pragma mark ---- 发送注册请求
-(void)startRegist{
    //1.创建会话管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //创建字典,加载参数
    NSDictionary * dict = @{
                            @"name":_userName,
                            @"pwd":_pwd,
                            @"type":@"JSON"
                            };
    
    //2.发送请求
    /*
     第一个参数:请求路径(1不包含参数2?3NSString)
     第二个参数:字典
     第三个参数:progress 进度
     第四个参数:success 成功回调
     task :请求任务 task.response
     responseObject:响应体信息(内部已经完成了序列化处理.JSON)
     第五个参数:failure 失败的回调
     error:错误信息
     */
    //这一步不能少
    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];
    
    [manager GET:@"http://api.liyaogang.com/register.php" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //3.解析
        NSLog(@"注册成功%@--%@--%@",[responseObject class],responseObject,[NSThread currentThread]);
        
        //4.跳转到登陆界面
            [SVProgressHUD showSuccessWithStatus:@"注册成功"];
            [self performSegueWithIdentifier:@"reg" sender:nil];
      
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"注册失败--%@",error);
        [SVProgressHUD showErrorWithStatus:@"注册失败"];
    }];
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
